package com.ebt.m.customer.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.customer.item.Contact;
import com.ebt.m.customer.view.AvatarView;

import java.util.HashMap;
import java.util.List;


public class AdapterContact extends BaseAdapter implements View.OnClickListener{

	private LayoutInflater inflater;
	private List<Contact> contacts;
	
	private HashMap<Integer, Integer> sectionToPosition = new HashMap();
	
	public AdapterContact(Context context, List<Contact> contacts){
		super();
		this.contacts = contacts;
		this.inflater = LayoutInflater.from(context);
		if(sectionToPosition != null){
			sectionToPosition.clear();
		}
	}

	@Override
	public int getCount() {
		return contacts.size();
	}

	@Override
	public Object getItem(int position) {
		return contacts.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

    ListView listView;
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(listView == null){
            listView = (ListView)parent;
        }
        ViewHolder viewHolder = null;
        if (null == convertView){
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.view_customer_item_from_contact, null);
            viewHolder.iv_avatar = (AvatarView) convertView.findViewById(R.id.iv_avatar);
            viewHolder.view_item_customer_from_contact_tv_sort_key = (TextView) convertView.findViewById(R.id.view_item_customer_from_contact_tv_sort_key);
//            viewHolder.view_item_customer__from_contact_iv_avatar = (ImageView) convertView.findViewById(R.id.view_item_customer_from_contact_iv_avatar);
            viewHolder.view_item_customer_from_contact_tv_name = (TextView) convertView.findViewById(R.id.view_item_customer_from_contact_tv_name);
            viewHolder.view_item_customer_from_contact_tv_imported = (TextView) convertView.findViewById(R.id.view_item_customer_from_contact_tv_imported);
            viewHolder.view_item_customer_from_contact_ib_check = (ImageView) convertView.findViewById(R.id.view_item_customer_from_contact_ib_check);

            convertView.setTag(viewHolder);
        }
        else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Contact contact = (Contact)getItem(position);
        if (contact != null){
            viewHolder.view_item_customer_from_contact_tv_sort_key.setText(String.valueOf(contact.getSortKeyChar()));
            viewHolder.view_item_customer_from_contact_tv_name.setText(contact.getName());
            if(contact.hasImported || contact.hasIgnored){
            	viewHolder.view_item_customer_from_contact_ib_check.setVisibility(View.GONE);
                if(contact.hasIgnored){
                    if(((ListView)parent).isItemChecked(position)) {
                        setViewIgnoreToAdd((ViewGroup) convertView);
                    }else{
                        setViewIgnoreToIgnore((ViewGroup) convertView, position);
                    }
                }else{
                    viewHolder.view_item_customer_from_contact_tv_imported.setVisibility(View.VISIBLE);
                    viewHolder.view_item_customer_from_contact_tv_imported.setText("已导入");
                    viewHolder.view_item_customer_from_contact_tv_imported.setTag(null);
                    viewHolder.view_item_customer_from_contact_tv_imported.setOnClickListener(null);
                }
            }else{
            	viewHolder.view_item_customer_from_contact_tv_imported.setVisibility(View.GONE);
            	viewHolder.view_item_customer_from_contact_ib_check.setVisibility(View.VISIBLE);
            	//判断check的状态，如果是勾选的，就勾选上
            	if(((ListView)parent).isItemChecked(position)){
            		viewHolder.view_item_customer_from_contact_ib_check.setImageResource(R.drawable.compoundbutton_checked);
            	}else{
            		viewHolder.view_item_customer_from_contact_ib_check.setImageResource(R.drawable.compoundbutton_unchecked);
            	}
            }
            if(TextUtils.isEmpty(contact.name) || TextUtils.isEmpty(contact.name.trim())){
                viewHolder.iv_avatar.setTextAndColorSeed(" "," ");
            }else{
                viewHolder.iv_avatar.setTextAndColorSeed(contact.name.substring(0, 1), contact.name);
            }
        }

        int section = getSectionForPosition(position);
		if (position == getPositionForSection(section)) {
			viewHolder.view_item_customer_from_contact_tv_sort_key.setText(String.valueOf(contact.getSortKeyChar()));
			if(viewHolder.view_item_customer_from_contact_tv_sort_key.getVisibility() != View.VISIBLE){
				viewHolder.view_item_customer_from_contact_tv_sort_key.setVisibility(View.VISIBLE);
			}
		} else {
			if(viewHolder.view_item_customer_from_contact_tv_sort_key.getVisibility() != View.GONE){
				viewHolder.view_item_customer_from_contact_tv_sort_key.setVisibility(View.GONE);
			}
		}
        return convertView;
	}

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.view_item_customer_from_contact_tv_imported:
                if(v.getTag()!=null){
                    setIgnoreToAdd((int)v.getTag());
                }
            break;
        }
    }

    private void setIgnoreToAdd(int position){
        listView.setItemChecked(position, true);
        if(listView.getFirstVisiblePosition() <= position && position <= listView.getLastVisiblePosition()){
            ViewGroup child = (ViewGroup)listView.getChildAt(position - listView.getFirstVisiblePosition());
            setViewIgnoreToAdd(child);
        }
    }

    private void setViewIgnoreToAdd(ViewGroup child){
        child.findViewById(R.id.view_item_customer_from_contact_tv_imported).setVisibility(View.GONE);

        ImageView iv = (ImageView)child.findViewById(R.id.view_item_customer_from_contact_ib_check);
        iv.setVisibility(View.VISIBLE);
        iv.setImageResource(R.drawable.compoundbutton_checked);
    }

    private void setViewIgnoreToIgnore(ViewGroup child, int position){
        child.findViewById(R.id.view_item_customer_from_contact_ib_check).setVisibility(View.GONE);

        TextView imported = (TextView)child.findViewById(R.id.view_item_customer_from_contact_tv_imported);
        imported.setVisibility(View.VISIBLE);
        imported.setVisibility(View.VISIBLE);
        imported.setText("取消忽略");
        imported.setTag(position);
        imported.setOnClickListener(this);
    }

    /*private boolean checkIfIgnoreToAdd(int position){
        Boolean b = mIgnoreToAdd.get(position);
        if(b != null && b){
            return true;
        }
        return false;
    }*/

//    private void setIgnoreToAddCache(int position, boolean ifAdd){
//        mIgnoreToAdd.put(position, ifAdd);
//    }

    private static class ViewHolder{
        AvatarView iv_avatar;
    	//上方的sort key
        TextView view_item_customer_from_contact_tv_sort_key;
        //名字
        TextView view_item_customer_from_contact_tv_name;
        //状态：已导入
        TextView view_item_customer_from_contact_tv_imported;
        //状态：未导入-是否选中
        ImageView view_item_customer_from_contact_ib_check;

    }

    /*private static class TagWrapper{
        public Object obj0; //contact
        public Object obj1; //position

        public TagWrapper(Object obj0, Object obj1){
            this.obj0 = obj0;
            this.obj1 = obj1;
        }

        public TagWrapper(){
        }
    }*/

    public int getSectionForPosition(int position) {
    	return contacts.get(position).sortKeyChar;
    }

	/**
	 * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
	 */
    public int getPositionForSection(int section) {
    	
    	Integer p = sectionToPosition.get(section);
    	if(p != null) {
    		return p;
    	}
		int size = contacts.size();
		for (int i = 0; i < size; i++) {
			if (contacts.get(i).sortKeyChar == section) {
				sectionToPosition.put(section, i);
				return i;
			}
		}
		return -1;
	}
    
    public void clearSectionToPositionCache(){
    	if(sectionToPosition != null){
			sectionToPosition.clear();
		}
    }
    
}
